package com.qf.blog.controller;

import com.qf.blog.dto.PostDto;
import com.qf.blog.entity.DiscussPost;
import com.qf.blog.entity.Invitation;
import com.qf.blog.entity.User;
import com.qf.blog.service.CommentService;
import com.qf.blog.service.DiscussPostService;
import com.qf.blog.service.IInvitationService;
import com.qf.blog.service.UserService;
import com.qf.blog.service.impl.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * User: key
 * ClassName:SystemController
 * Date: 2023/6/14
 * Time: 14:02
 * description:
 */
@RestController
@RequestMapping("/sys/post")
public class SystemController {

    @Autowired
    private LikeService likeService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private DiscussPostService postService;

    @GetMapping("/list")
    public List<PostDto> list() {

        List<PostDto> postDtos = new ArrayList<>();

        // 1.查询所有的帖子
        List<DiscussPost> list = postService.list();

        // 2.遍历
        for (DiscussPost invitation : list) {

            // 3.查询帖子点赞数量
            Long likeCount = likeService.getLikeCount(1, invitation.getId());

            // 4.查询帖子评论数量
            Long commentCount = commentService.getCommentCount(invitation.getId());

            // 5.查询作者
            User user = userService.getById(invitation.getUserId());

            PostDto postDto = new PostDto();
            postDto.setTitle(invitation.getTitle());
            postDto.setId(invitation.getId());
            postDto.setCommentCount(commentCount);
            postDto.setLikeCount(likeCount);
            postDto.setUsername(user.getUsername());
            postDto.setType(invitation.getType());
            postDto.setStatus(invitation.getStatus());


            postDtos.add(postDto);
        }

        return postDtos;
    }
    @GetMapping("/digest/{id}")
    public List<PostDto> top1(@PathVariable("id") Integer id){
        DiscussPost post = postService.getById(id);
        Integer status = post.getStatus();
        if (status != null) {
            if(status != 0){
                post.setStatus(0);
            }else {
                post.setStatus(1);
            }
        }else {
            post.setStatus(1);
        }
        postService.updatePostStatus(id,post.getStatus());

        //存入数据库后再进行查询
        List<PostDto> postDtos = new ArrayList<>();

        // 1.查询所有的帖子
        List<DiscussPost> list = postService.list();

        // 2.遍历
        for (DiscussPost invitation : list) {

            // 3.查询帖子点赞数量
            Long likeCount = likeService.getLikeCount(1, invitation.getId());

            // 4.查询帖子评论数量
            Long commentCount = commentService.getCommentCount(invitation.getId());

            // 5.查询作者
            User user = userService.getById(invitation.getUserId());

            PostDto postDto = new PostDto();
            postDto.setTitle(invitation.getTitle());
            postDto.setId(invitation.getId());
            postDto.setCommentCount(commentCount);
            postDto.setLikeCount(likeCount);
            postDto.setUsername(user.getUsername());
            postDto.setType(invitation.getType());
            postDto.setStatus(invitation.getStatus());


            postDtos.add(postDto);
        }
        return postDtos;
    }

    @GetMapping("/top/{id}")
    public List<PostDto> digest1(@PathVariable("id") Integer id){
        DiscussPost post = postService.getById(id);
        Integer type = post.getType();
        if (type != null) {
            if(type != 0){
                post.setType(0);
            }else{
                post.setType(1);
            }
        }else {
            post.setType(1);
        }
        postService.updatePostType(id,post.getType());

        //存入数据库后再进行查询
        List<PostDto> postDtos = new ArrayList<>();

        // 1.查询所有的帖子
        List<DiscussPost> list = postService.list();

        // 2.遍历
        for (DiscussPost invitation : list) {

            // 3.查询帖子点赞数量
            Long likeCount = likeService.getLikeCount(1, invitation.getId());

            // 4.查询帖子评论数量
            Long commentCount = commentService.getCommentCount(invitation.getId());

            // 5.查询作者
            User user = userService.getById(invitation.getUserId());

            PostDto postDto = new PostDto();
            postDto.setTitle(invitation.getTitle());
            postDto.setId(invitation.getId());
            postDto.setCommentCount(commentCount);
            postDto.setLikeCount(likeCount);
            postDto.setUsername(user.getUsername());
            postDto.setType(invitation.getType());
            postDto.setStatus(invitation.getStatus());


            postDtos.add(postDto);
        }
        return postDtos;
    }
}
